package com.hl.controller;

import com.github.pagehelper.PageHelper;
import com.hl.domain.Free;
import com.hl.service.FreeService;
import com.hl.service.serviceImpl.FreeServiceImpl;
import com.hl.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RequestMapping("/free")
@RestController
public class FreeController {
    @Autowired
    private FreeServiceImpl freeService;
    @GetMapping("/findAllFree")
    public R findAllFree(int pageNum,int pageSize ){
        PageHelper.startPage(pageNum,pageSize);
        List<Free> frees = freeService.findAllFree();
        if (frees.size()>0){
            return R.success(frees);

        }else {
            return R.fail("");
        }
    }
    @GetMapping("/findFreelist")
    public R findFreelist( ){
        List<Free> frees = freeService.findAllFree();
        if (frees.size()>0){
            return R.success(frees);

        }else {
            return R.fail("");
        }
    }


    //模糊搜索
    @GetMapping("/selectFreeLike")
    public R selectFreeLike(String name){
        List<Free> frees = freeService.selectFreeLike(name);
        if (frees.size()>0){
            return R.success(frees);

        }else{
            return R.fail("");
        }
    }
    //删除
    @DeleteMapping("/delFree")
    public R    deleteByPrimaryKey(int id){
        int i = freeService.deleteByPrimaryKey((long) id);
        if (i==1){
            return R.success(id);
        }else {
            return R.fail("");

        }
    }
    //修改
    @PostMapping("/updateFree")
    public R   updateByPrimaryKeySelective(Free record){
        int i = freeService.updateByPrimaryKeySelective(record);
        if (i==1){
            return R.success(record);
        }else {
            return R.fail("");
        }
    }
    @PostMapping("/insertFree")
    public R insertSelective(Free record){
        record.setCreatetime(new Date());
        int i = freeService.insertSelective(record);
        if (i==1){
            return R.success(record);
        }else {
            return R.fail("");
        }



    }

    @PostMapping("/upload")
    public R   upload(MultipartFile file) throws IOException {
        String  path="D:\\upload\\";

        String originalFilename = file.getOriginalFilename();
        String ext=originalFilename.substring(originalFilename.lastIndexOf("."));
        String  filename= UUID.randomUUID().toString()+ext;
        path+=filename;

        file.transferTo(new File(path));

        return  R.success(filename);
    }
    @GetMapping("/down")
    public  void   down(String  filename, HttpServletResponse response) throws IOException {
        FileInputStream in=new FileInputStream("D:\\upload\\"+filename);
        ServletOutputStream out = response.getOutputStream();

        byte[]  b=new  byte[1024];
        int  i=in.read(b);
        while(i!=-1){
            out.write(b,0,i);
            i=in.read(b);
        }
        out.close();
        in.close();
    }
    @GetMapping("/findByIdFree")
    public R findById(int id){
        Free free = freeService.selectByPrimaryKey((long) id);
        if (free!=null){
            return R.success(free);
        }else {
            return R.fail("");
        }
    }





}
